package com.gitee.cashzhang27.test.cloud.oauth.resource.server.controller;

import com.gitee.cashzhang27.test.cloud.oauth.resource.server.entity.UserDetails;
import com.gitee.cashzhang27.test.cloud.oauth.resource.server.util.LocalUserContext;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/02/08 19:37
 */
@RestController
@RequestMapping("user")
public class UserController {

  @GetMapping("getUser")
  public UserDetails getUser(@RequestParam("username") String username) {
    return UserDetails.builder().id("1").username(username)
        .password("$2a$10$o4rEubZWoDRz0XqtXOKK9uhRRb9eo6On5lpekJ5EpC5Et9BLMbdwW").roleId("1")
        .build();
  }

  @GetMapping("/userInfo")
  public ResponseEntity<UserDetails> getUserInfo() {

    String userId = LocalUserContext.getUserId();
    UserDetails userDetails = UserDetails.builder().id(userId).build();

    return ResponseEntity.ok(userDetails);
  }

  @GetMapping("/me")
  public Authentication me(OAuth2Authentication user) {
    // 获得自己实现的类
//    UserDetails principal = (UserDetails) user.getPrincipal();
    return user;
  }
}
